﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IsikFx.Project.ClientApp.Helpers;
using IsikFx.Project.CommonTypes.CRUD;
using IsikFx.Project.CommonTypes.FundLog;

namespace IsikFx.Project.ClientApp.Content
{
    /// <summary>
    /// Interaction logic for FundOut.xaml
    /// </summary>
    public partial class FundOut : UserControl
    {
        public FundOut()
        {
            InitializeComponent();
            UpdateHistory();
        }

        private void btnFundOut_Click_1(object sender, RoutedEventArgs e)
        {
            string hour = DateTime.Now.Hour.ToString();
            string minute = DateTime.Now.Minute.ToString();
            string second = DateTime.Now.Second.ToString();
            CrudResponseMessage response = Helper.AddNewFundLog(Helper.CurrentUser.UserID, txtFundOut.Text, 1, DateTime.Today.ToString(), string.Format("{0}:{1}:{2}", hour, minute, second));
            if (response.HasError)
            {
                Helper.ShowMessage(response.ErrorMessage);
            }
            else 
            {
                Helper.ShowMessage(txtFundOut.Text + " USD Fon Çıkışı İşlemi Başarıyla gerçekleşti.");
                UpdateHistory();
            }
        }
        private void UpdateHistory()
        {
            List<FundLog> fundLogList = Helper.GetMinusFunds(Helper.CurrentUser.UserID, DateTime.Today.ToString()).Response.FundLogs;
            List<ManipulatedItemSource> itemSource = new List<ManipulatedItemSource>();
            if (fundLogList == null || fundLogList.Count == 0)
                return;
            fundLogList = fundLogList.OrderByDescending(t => t.FundTime).ToList();
            foreach (FundLog fundLog in fundLogList)
            {
                ManipulatedItemSource item = new ManipulatedItemSource();
                item.FundOut = String.Format("-{0} USD--{1}", fundLog.FundAmount, fundLog.FundTime);
                itemSource.Add(item);
            }
            FundOutLogs.ItemsSource = null;
            FundOutLogs.ItemsSource = itemSource;
        }

        internal class ManipulatedItemSource
        {
            public string FundOut { get; set; }
        }
    }
}
